package cn.com.utils;

import cn.com.v1alpha1.Tensorboard;
import cn.com.v1alpha1.TensorboardSpec;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PodUtil {
    private static final String TOKEN = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjgySUFDSWkzMFdoZjJuYmg2LXJPdDZPdWd2SXNnVDFOalF1bXNVNDN0UncifQ.eyJpc3MiOiJrd"
            + "WJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWN"
            + "lYWNjb3VudC9zZWNyZXQubmFtZSI6ImFkbWluLXNlcnZpY2VhY2NvdW50LXRva2VuLTd0YnFyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY"
            + "291bnQubmFtZSI6ImFkbWluLXNlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNjMxZDNjNWYtOTg"
            + "0My00Yzc3LWFhM2EtN2YzNTkyYzVlYzYzIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmRlZmF1bHQ6YWRtaW4tc2VydmljZWFjY291bnQifQ.lvrvqTgUGGAe2G"
            + "ZJrdVe_NXL-Q5scGkacslPFCV0A3jLyK4rgY91q69wGjD9jEQ_hlJ2xxU4r2XHMeHEokkqcGlR5LP3t9rWAMQ10iySKTFyFG3UDMNbOTsoVIRY9LxSnOnBF-sHUj8WyizA"
            + "YyKfEd-5MwwKUk5rve_2oK-3YPsAoTwY9VJTQOgsjYU69qx9IyJjBAdFZTM9Q9JNJEkcHXqrGnECdovdWNJP5F_mwo8fCsFXoGzH18syskxQNp6zNsUy6R53_JWvo_XlXi"
            + "2VWCt8VmptUGC43HaQbmHRUCP09z9G75W-2bPmslnCeGz4UBZ1W24fo2ems_okBkAuqg";

    private static final String BASE_URL = "https://10.106.3.111:6443";
    private static final KubernetesClient kubernetesClient;

    static {

        Config config = new ConfigBuilder()
                .withTrustCerts(true)
                .withMasterUrl(BASE_URL)
                .withOauthToken(TOKEN)
                .build();
        //ObjectMapper mapper = PatchUtils.patchMapper();
        //mapper.registerModule(new JavaTimeModule());
        //mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        kubernetesClient = new DefaultKubernetesClient(config);
    }

    public static void main(String[] args) {
        PodUtil podUtil = new PodUtil();
        String jobName = "tensorboard-test-15";
        String kubeflow = "kubeflow";
        //podUtil.create(jobName, kubeflow);
        //podUtil.close(jobName, kubeflow);
        //podUtil.stop(jobName, kubeflow);
        podUtil.start(jobName, kubeflow);
    }


    /**
     * 创造
     */
    public void create(String jobName, String namespace) {

        try {
            Tensorboard tensorboard = new Tensorboard();
            tensorboard.getMetadata().setName(jobName);
            tensorboard.getMetadata().setNamespace(namespace);
            TensorboardSpec spec = new TensorboardSpec();
            tensorboard.setSpec(spec);
            spec.setAk("308e85afe37647dd8edc4816845c172f");
            spec.setBucket("308e85afe37647dd8edc4816845c172f-bucket");
            spec.setEndpoint("https://minio.cfn-20230109.com:8444");
            spec.setLogspath("trainingJob/1411070709899264/log/train");
            spec.setSk("acc60551156a4c8884aeee4ee7da90a0");
            spec.setType("tensorboard");
            spec.setReplicas("1");

            final MixedOperation<Tensorboard, KubernetesResourceList<Tensorboard>, Resource<Tensorboard>> resourceClient =
                    kubernetesClient.resources(Tensorboard.class);
            resourceClient.inNamespace(namespace).createOrReplace(tensorboard);
        } catch (Exception e) {
            log.error("创建Visualized服务时出错", e);

        }
    }

    /**
     * 关闭
     *
     * @param jobName 作业名称
     * @return boolean
     */
    public boolean close(String jobName, String namespace) {
        try {
            final MixedOperation<Tensorboard, KubernetesResourceList<Tensorboard>, Resource<Tensorboard>> resourceClient =
                    kubernetesClient.resources(Tensorboard.class);
            resourceClient.inNamespace(namespace).withName(jobName).delete();
            return true;
        } catch (Exception e) {
            log.error("关闭Visualized服务时出错", e);
            return false;
        }
    }

    /**
     * 停止
     *
     * @param jobName 作业名称
     * @return boolean
     */
    public boolean stop(String jobName, String namespace) {
        try {
            final MixedOperation<Tensorboard, KubernetesResourceList<Tensorboard>, Resource<Tensorboard>> resourceClient =
                    kubernetesClient.resources(Tensorboard.class);
            Tensorboard tensorboard = resourceClient.inNamespace(namespace).withName(jobName).get();
            tensorboard.getSpec().setReplicas(String.valueOf(0));
            resourceClient.inNamespace(namespace).withName(jobName).replace(tensorboard);
            return true;
        } catch (Exception e) {
            log.error("停止Visualized服务时出错", e);
            return false;
        }
    }

    /**
     * 开始
     *
     * @param jobName 作业名称
     * @return boolean
     */
    public boolean start(String jobName, String namespace) {
        try {
            final MixedOperation<Tensorboard, KubernetesResourceList<Tensorboard>, Resource<Tensorboard>> resourceClient =
                    kubernetesClient.resources(Tensorboard.class);
            Tensorboard tensorboard = resourceClient.inNamespace(namespace).withName(jobName).get();
            tensorboard.getSpec().setReplicas(String.valueOf(1));
            resourceClient.inNamespace(namespace).withName(jobName).replace(tensorboard);
            return true;
        } catch (Exception e) {
            log.error("启动Visualized服务时出错", e);
            return false;
        }
    }

}
